<template>
  <div>
    <el-button type="text" @click="dialogFormVisible = true; operateType = 'save'">添加品牌</el-button>
    <el-dialog title="品牌信息" :visible.sync="dialogFormVisible" @close="handleClose">
      <el-form :model="brand" accept-charset="UTF-8">
        <el-form-item label="品牌编号" :label-width="formLabelWidth">
          <el-input type="text" v-model="brand.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称" :label-width="formLabelWidth">
          <el-input v-model="brand.brand_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌类型" :label-width="formLabelWidth">
          <el-input v-model="brand.brand_type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌图片" :label-width="formLabelWidth">
          <el-upload class="avatar-uploader" action="http://localhost:8080/upload" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="brand.brand_img" :src="brand.brand_img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" fixed="right" width="150">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handlesave">确 定</el-button>
      </div>
    </el-dialog>

    <!--数据表格-->
    <el-table :data="brands" style="width: 100%">
      <el-table-column label="品牌编号" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌名称" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.brand_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌类型" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.brand_type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌图片" min-width="100">
        <template slot-scope="scope">
          <img v-if="scope.row.brand_img" :src="scope.row.brand_img" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="200">
        <template slot-scope="scope">
          <div style="display: flex; justify-content: space-around;">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination background layout="prev, pager, next" :page-size="pageSize" :total="total" :current-page="current"
      @current-change="loadBrands">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'Brand',
  data() {
    return {
      imageUrl: '',
      brands: [],  //品牌数组
      dialogFormVisible: false, //控制对话框隐藏显示
      formLabelWidth: '120px',
      brand: { id: '', brand_name: '', brand_type: '', brand_img: '' }, //品牌数据
      operateType: '', //操作类型   save update
      total: 0,     //总页数
      pageSize: 5,  //每一页行数
      current: 0   //当前页数
    }
  },
  methods: {
    //加载品牌列表的方法
    loadBrands(current) {
      this.current = current
      this.$http.get('/brand?m=page&current=' + this.current + "&pageSize=" + this.pageSize).then(resp => {
        console.log(resp.data)
        if (resp.data.data) {
          this.brands = resp.data.data.records
          this.total = resp.data.data.total
        }
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.operateType = 'update';
      //显示对话框，绑定数据
      this.dialogFormVisible = true;
      //进行深克隆 row---->string---->json
      this.brand = JSON.parse(JSON.stringify(row));
    },
    handlesave() {
      console.log('save brand', this.brand, this.$qs.stringify(this.brand))
      if (this.operateType == 'save') {
        //添加品牌
        this.$http.post('/brand?m=save', this.$qs.stringify(this.brand))
          .then(resp => {
            console.log('save brand', resp.data)
            if (resp.data.data == true) {
              //添加成功，刷新品牌列表
              this.loadBrands(this.current);
            }
          });
      } else {
        //更新用户
        this.$http.post('/brand?m=update', this.$qs.stringify(this.brand))
          .then(resp => {
            console.log('update brand', resp.data)
            if (resp.data.data == true) {
              //添加成功，刷新用户列表
              this.loadBrands(this.current);
            }
          })
      }
      this.dialogFormVisible = false
    },
    handleDelete(index, row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/brand?m=remove&id=' + row.id).then(resp => {
          if (resp.data.data == true) {
            this.loadBrands(this.current);
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      console.log(index, row);
    },
    handleClose() {
      this.brand = {
        id: '',
        brand_name: '',
        brand_type: '',
        brand_img: ''
      }
      this.operateType = ''
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!');
      }
      return (isJPG || isPNG) && isLt2M;
    },
    handleAvatarSuccess(res, file) {
      console.log('upload', res.data)
      this.brand.brand_img = res.data
    },
  },
  mounted() {
    this.loadBrands(1)
  }
}
</script>

<style scoped>
.el-table .cell {
  background-color: pink;
  white-space: normal !important;
  word-break: break-all;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
}

.avatar {
  width: 40px;
  height: 40px;
  display: block;
}

.operation-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.operation-buttons .el-button {
  margin: 5px 0;
}
</style>